package com.zgxt.demo.service.impl;


import com.zgxt.demo.entity.Role;
import com.zgxt.demo.entity.User;
import com.zgxt.demo.mapper.UserMapper;
import com.zgxt.demo.service.UserService;
import com.zgxt.demo.util.Conversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    //时间类
    Conversion conversion=new Conversion();

    @Autowired
    UserMapper userMapper;


    @Override
    public User login(String account, String password, Integer role_id) {
        return userMapper.login(account,password,role_id);
    }

    @Override
    public List<Role> RoleList() {
        return userMapper.RoleList();
    }

    @Override
    public Integer register(String name, String address, String password) {
        if (userMapper.selectAddress(address)!=null){
            return 2;
        }
        userMapper.register(name,address,password,conversion.Time());
        return 1;
    }

    @Override
    public Integer forget(String address, String password, String confirm_password) {
        if (!password.equals(confirm_password)){
            return 2;
        }
        if (password.equals(userMapper.selectPassword(address))){
            return 3;
        }

        return userMapper.forget(address,password,conversion.Time());
    }

    @Override
    public Map<String, Object> QueryUser(Integer pageIndex, Integer pageSize, String name) {
        Map<String,Object> map=new HashMap<>();
        map.put("data",userMapper.QueryUser(pageIndex,pageSize,name));
        map.put("count",userMapper.UserCount(name));
        return map;
    }

    @Override
    public Integer deleteUser(String ids) {
        return userMapper.deleteUser(ids.split(","),conversion.Time());
    }

    @Override
    public Integer updateUser(Integer id, String name, String password) {
        return userMapper.updateUser(id,name,password,conversion.Time());
    }
}
